package com.dandev.cmdemo.form;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.validator.constraints.NotEmpty;


/**
 * A contact can be a person or institution.  The only required field in last name, 
 * which is group name in the case of an institution.  Contacts are one-to-many self-referential.
 * 
 * @author Dan
 */
@Entity
@Table(name="contact")
public class Contact {
	@Id
    @Column(name="id")
    @GeneratedValue
	private Long id;
	
	@Column(name="first_name")
	private String firstName;
	
	@Column(name="last_name")
	@NotEmpty
	private String lastName;
	
	@Column(name="work_phone")
	private String workPhone;
	
	@Column(name="cell_phone")
	private String cellPhone;
	
	@Column(name="email")
	private String email;
	
	@Column(name="alt_email")
	private String altEmail;
	
	@Column(name="url")
	private String url;
	
	@Column(name="tag_id")
	private Long tagId;
	
	private transient Long parentId;
	
	@ManyToOne(optional = true)
    @JoinColumn(name="parent_id")
	private Contact parent;
	
	@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
	private List<Contact> contactList = new ArrayList<Contact>();
	
	@OneToMany (cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, mappedBy="parentContact")
	private List<Communique> commList = new ArrayList<Communique>();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getWorkPhone() {
		return workPhone;
	}

	public void setWorkPhone(String workPhone) {
		this.workPhone = workPhone;
	}

	public String getCellPhone() {
		return cellPhone;
	}

	public void setCellPhone(String cellPhone) {
		this.cellPhone = cellPhone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAltEmail() {
		return altEmail;
	}

	public void setAltEmail(String altEmail) {
		this.altEmail = altEmail;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Long getTagId() {
		return tagId;
	}

	public void setTagId(Long tagId) {
		this.tagId = tagId;
	}

	public List<Contact> getContactList() {
		return contactList;
	}

	public void setContactList(List<Contact> contactList) {
		this.contactList = contactList;
	}
	
	public Long getParentId() {
		if(parentId != null) {
			return parentId;
		} else if(parent != null) {
			parentId = parent.getId();
			return parentId;
		} 
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public Contact getParent() {
		return parent;
	}

	public void setParent(Contact parent) {
		this.parent = parent;
	}
	
	public List<Communique> getCommList() {
		return commList;
	}

	public void setCommList(List<Communique> commList) {
		this.commList = commList;
	}
	
	public void addCommunique(Communique comm) {
		commList.add(comm);
	}
	
	/**
	 * To be used with a persistence manager.  After a instance is loaded from the 
	 * persistence manager use this method update instance.
	 * @param form updated from.
	 */
	public void update(Contact form) {
		setFirstName(form.getFirstName());
		setLastName(form.getLastName());
		setWorkPhone(form.getWorkPhone());
		setCellPhone(form.getCellPhone());
		setEmail(form.getEmail());
		setAltEmail(form.getAltEmail());
		setUrl(form.getUrl());
		setTagId(form.getTagId());
	}

	@Override
	public String toString() {
		return "Contact [id=" + id + ", firstName=" + firstName + ", lastName="
				+ lastName + ", workPhone=" + workPhone + ", cellPhone="
				+ cellPhone + ", email=" + email + ", altEmail=" + altEmail
				+ ", url=" + url + ", tagId=" + tagId + ", parentId="
				+ parentId + "]";
	}
	

}